package pl.rzarajczyk.breaktime.utils;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import javax.swing.JFrame;

/**
 *
 * @author rafalz
 */
public class ExtendedJFrame extends JFrame {
    
    private GraphicsDevice gd;
    
    private void initializeGraphicsDevice() {
        if ( gd == null ) {
            gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
        }
    }
    
    public GraphicsDevice getGraphicsDevice() {
        initializeGraphicsDevice();
        return gd;
    }
    
    public void setGraphicsDevice(GraphicsDevice device) {
        gd = device;
    }
    
    // =========================================================================

    public ExtendedJFrame(String string, GraphicsConfiguration gc) {
        super(string, gc);
    }

    public ExtendedJFrame(String string) throws HeadlessException {
        super(string);
    }

    public ExtendedJFrame(GraphicsConfiguration gc) {
        super(gc);
    }

    public ExtendedJFrame() throws HeadlessException {
    }
    
    private boolean isFullScreen = false;
    
    public void setFullScreen(boolean enabled) {
        initializeGraphicsDevice();
        boolean visible = this.isVisible();
        if ( isFullScreen != enabled ) {
            this.dispose();
            if ( enabled ) {
                setUndecorated(true);
                getGraphicsDevice().setFullScreenWindow(this);
            } else {
                setUndecorated(false);
                getGraphicsDevice().setFullScreenWindow(null);
            }
            if ( visible ) {
                this.setVisible(true);
            }
            isFullScreen = enabled;
        }
    }
    
    public boolean isFullScreen() {
        return isFullScreen;
    }
}
